<?php


namespace app\admin\service\base;


use app\admin\model\base\Paper;
use app\admin\model\base\Practise;
use app\admin\model\base\Student;
use app\admin\model\base\StudentPaper;
use app\admin\model\base\Subject;
use app\admin\model\StudentPractise;
use app\BaseService;
use think\facade\Db;
use think\response\Json;

class IndexService extends BaseService
{
    /**
     * 首页统计数据
     * @return Json
     */
    public function index():Json
    {
        $data = [];
        //总试卷数
        $data['paper_num'] = Paper::where('is_del','=',0)->where('is_release','=',1)->count();
        //总练习数
        $data['practise_num'] = Practise::where('is_del','=',0)->where('is_release','=',1)->count();
        //总学生数
        $data['student_num'] = Student::where('is_del','=',0)->count();
        //总题目数
        $data['subject_num'] = Subject::where('is_del','=',0)->count();
        //考试统计数据
        $data['student_paper_num'] = StudentPaper::where('is_del','=',0)->count();
        $data['student_paper_week'] = StudentPaper::where('is_del','=',0)->whereTime('created_at','week')->count();
        $data['student_paper_month'] = StudentPaper::where('is_del','=',0)->whereTime('created_at','month')->count();

        //练习统计数据
        $data['student_practise_num'] = StudentPractise::where('is_del','=',0)->count();
        $data['student_practise_week'] = StudentPractise::where('is_del','=',0)->whereTime('created_at','week')->count();
        $data['student_practise_month'] = StudentPractise::where('is_del','=',0)->whereTime('created_at','month')->count();

        return jsonReturn(1, $data);
    }

    /**
     * 获取配置
     * @param array $params
     * @return Json
     */
    public function config(array $params):Json
    {
        $data['subject_template'] = request()->domain().'/template/题目导入excel模板.xlsx';
        return jsonReturn(1, $data);
    }

    /**
     * 条形统计图
     * @return Json
     */
    public function statistics(array $params):Json
    {
        $type = $params['type'] ?? 1;//1是考试2是练习
        $end_time = date('Y-m-d',strtotime(date('Y-m-d')));
        $start_time = date('Y-m-d',strtotime(date('Y-m-d')) - (15 * 86400));
        if(isset($params['start_time']) && $params['start_time'] && isset($params['end_time']) && $params['end_time']){
            $end_time = date('Y-m-d',strtotime($params['end_time']));
            $start_time = date('Y-m-d',strtotime($params['start_time']));
        }
        if($type == 1){
            //考试
            $list = Db::query("SELECT DATE(created_at) as date, COUNT(1) as count
                        FROM `student_paper`
                        WHERE is_del = 0
                          AND DATE_FORMAT(created_at, '%Y-%m-%d') >=
                              '$start_time'
                          AND DATE_FORMAT(created_at, '%Y-%m-%d') <=
                              '$end_time'
                        GROUP BY DATE(created_at)
                        ");
        }else{
            $list = Db::query("SELECT DATE(created_at) as date, COUNT(1) as count
                        FROM `student_practise`
                        WHERE is_del = 0
                          AND DATE_FORMAT(created_at, '%Y-%m-%d') >=
                              '$start_time'
                          AND DATE_FORMAT(created_at, '%Y-%m-%d') <=
                              '$end_time'
                        GROUP BY DATE(created_at)
                        ");
        }
        $arr = [];
        if(!empty($list)){
            foreach ($list as $value){
                $arr[$value['date']] = $value['count'];
            }
        }
        //计算天数，没有的补零
        $day = diff_date($end_time,$start_time);
        $dateName = [];
        $dateValue = [];
        for ($i = 0; $i <= $day; $i++){
            $date = date('Y-m-d',strtotime($start_time) + ($i*86400));
            $dateName[] = $date;
            if(isset($arr[$date])){
                $dateValue[] = $arr[$date];
            }else{
                $dateValue[] = 0;
            }
        }

        return jsonReturn(1, ['date_name'=>$dateName,'date_value'=>$dateValue]);
    }
}